#pragma once
#include "types.h"

class Transform
{	
public:
	Transform() 
	{  
		translation = XMVectorZero();
		rotation = XMQuaternionIdentity();
		scale = XMVectorSplatOne();
	}
	vec forward()
	{
		return quat::rotate(vec4::forward(), rotation);
	}
	vec up()
	{
		return quat::rotate(vec4::up(), rotation);
	}
	vec left()
	{
		return quat::rotate(vec4::left(), rotation);
	}
	void translate(vec t)
	{		
		translation += t;
	}
	void rotate(vec rotation_origin, float dheading, float dpitch, float droll)
	{
		Quat rot = XMQuaternionRotationRollPitchYaw(dpitch, dheading, droll);

		rotate(rotation_origin, rot);
	}
	void rotate_local(float dheading, float dpitch, float droll)
	{		
		Quat rot = XMQuaternionRotationRollPitchYaw(dpitch, dheading, droll);
		rotation = XMQuaternionMultiply(rotation, rot);
	}
	void rotate(vec axis, vec rotation_origin, float angle_in_degrees)
	{
		Quat rot = XMQuaternionRotationAxis(axis, XMConvertToRadians(angle_in_degrees));
		rotate(rotation_origin, rot);
	}
	void rotate(vec rotation_origin, Quat quat)
	{
		auto offset = translation - rotation_origin;
		rotation = XMQuaternionMultiply(quat, rotation);
		offset = quat::rotate(offset, quat);
		translation = rotation_origin + offset ;
	}
	void lookat(vec target, vec up)
	{
		vec dummy;
		XMMatrixDecompose(&dummy, &rotation, &dummy, XMMatrixLookAtLH(translation, target, up));
	}
	vec translation;
	Quat rotation;
	vec scale;
};